package com.amqp.consumer.receive;

import com.rabbitmq.client.*;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * @Author: sijunqiang
 * @Date: 2020/6/12 15:10
 * @Description: 该类是接收rabbitmq上面队列的消息
 */
@Slf4j
public class ReceiveMessage {


    private final static String QUEUE_NAME = "queue_name";


    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("rabbitmq上面的地址");
        // 创建一个连接节点资源对象
        Connection connection = factory.newConnection();
        //创建一个连接通道资源对象
        Channel channel = connection.createChannel();
        //声明一个队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        ///////////////////////////////////////////////
        // TODO: 2020/6/12  deliverCallBack是 代表什么意思 目前不太清楚
        // 好像是回调函数
        DeliverCallback deliverCallback = ((consumerTag, delivery) -> {
            log.info("打印delivery是个什么东西：[{}]", delivery.toString());
            // 将消息进行编码
            String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
        });
        // TODO: 2020/6/12  
        GetResponse getResponse = channel.basicGet(QUEUE_NAME, true);
        log.info("使用basicGet方法 确认消息确认机制：[{}]",getResponse.toString());

        // TODO: 2020/6/16  测试
        //channel.basicConsume()


        //进行回调信息
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
            log.info("打印consumerTag 这是什么意思：[{}]", consumerTag);
        });





    }


}
